package xyz.wanwanwan.www.hrm.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xyz.wanwanwan.www.hrm.CourseDoc;
import xyz.wanwanwan.www.hrm.query.CourseQuery;
import xyz.wanwanwan.www.hrm.repository.CourseElasticsearchRepository;
import xyz.wanwanwan.www.hrm.service.ISearchCourse;
import xyz.wanwanwan.www.hrm.util.AjaxResult;
import xyz.wanwanwan.www.hrm.util.PageList;

@RestController
public class CourseController {
    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;
    @Autowired
    private ISearchCourse searchCourse;
    @PostMapping("/es/save")
    public AjaxResult saveDoc(@RequestBody CourseDoc courseDoc){
        try {
            courseElasticsearchRepository.save(courseDoc);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("课程保存到es失败");
        }
    }
    @PostMapping("/es/del/{id}")
    public AjaxResult delDoc(@PathVariable("id") Long id){
        try {
            courseElasticsearchRepository.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("课程从es中删除失败");
        }
    }

    @PostMapping("/es/queryCourses")
    public AjaxResult queryCourse(@RequestBody CourseQuery query){
        try {
            PageList<CourseDoc> result =  searchCourse.queryCourse(query);
            return AjaxResult.me().setResultObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("条件查询课程失败了");
        }
    }
}
